package com.camunda7.workflow.controller;

import com.camunda7.workflow.models.Message;
import org.camunda.bpm.engine.HistoryService;
import org.camunda.bpm.engine.IdentityService;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.history.HistoricProcessInstance;
import org.camunda.bpm.engine.history.HistoricProcessInstanceQuery;
import org.camunda.bpm.engine.identity.User;
import org.camunda.bpm.engine.identity.UserQuery;
import org.camunda.bpm.engine.impl.persistence.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.xml.transform.Source;
import java.util.List;

@Controller
public class TestController {



    @Autowired
    private IdentityService identityService;

    @Autowired
    private RuntimeService runtimeService;


    @Autowired
    private HistoryService historyService;

    @RequestMapping(method = RequestMethod.GET,value = "/start/{processKey}")
    @ResponseBody
    public Message start(@PathVariable(value = "processKey") String processKey){

        identityService.setAuthenticatedUserId("admin");
        runtimeService.startProcessInstanceByKey(processKey);


        Message message = new Message();
        message.setContent("OK");
        return message;
    }


    @RequestMapping(method = RequestMethod.GET,value = "/foo")
    @ResponseBody
    public Message foo(){


        HistoricProcessInstanceQuery query = historyService.createHistoricProcessInstanceQuery();

        HistoricProcessInstanceQuery processQingjia = query.processDefinitionKey("process_qingjia");

        List<HistoricProcessInstance> list = processQingjia.list();

        for (int i = 0; i < list.size(); i++) {
            HistoricProcessInstance instance = list.get(i);
            System.out.println(instance.getProcessDefinitionName());
            System.out.println(instance.getId());
            System.out.println(instance.getStartUserId());
            System.out.println(instance.getStartTime());
            System.out.println(instance.getEndTime());
        }


        Message message = new Message();
        message.setContent("OK");
        return message;
    }



}
